package com.tools.s7.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/5/8.
 * String 相关类型处理工具
 */
public class StringUtils {

    public static void setToast(Context context, String str) {
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();
    }

    public static void setPushToast(Context context, String str) {
        setPushToast(context, null, str);
    }

    public static void setPushToast(Context context, String title, String str) {
        PushToast.makeText(context, title, str).show();
    }

    public static boolean isContain(String str, String s) {
        return str.contains(s);
    }

    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static String setHideMobileNO(String phone) {
        if (!TextUtils.isEmpty(phone)) {
            return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
        return null;
    }

    public static String settingEmail(String email) {
        String emails = email.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
        return emails;
    }

    public static String getString(Map<String, String> map, String sign) {
        if(map == null){
            return null;
        }
        java.util.Map.Entry entry;
        StringBuffer sb = new StringBuffer();
        for(Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
        {
            entry = (java.util.Map.Entry)iterator.next();
            sb.append(null==entry.getValue()?"":entry.getValue().toString()).append (iterator.hasNext() ? sign : "");
        }
        return sb.toString();
    }

    public static String getString(List<String> list, String sign) {
        if(list==null){
            return null;
        }
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for(String string :list) {
            if(first) {
                first = false;
            }else{
                result.append(sign);
            }
            result.append(string);
        }
        return result.toString();
    }

    public static List getList(String str, String flag) {
        if(str == null){
            return null;
        }
        String str2 = str.replace(" ", ""); //去掉所用空格
        List<String> list = Arrays.asList(str2.split(flag));
        return list;
    }

    public static List removeDuplicatListHashSet(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    public static List removeDuplicateList(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    public static<T> boolean isListEqual(List<T> datas1, List<T> datas2) {
        if (datas1 == datas2) {
            return true;
        }
        if ((datas1 == null && datas2 != null && datas2.size() == 0)
                || (datas2 == null && datas1 != null && datas1.size() == 0)) {
            return true;
        }
        if (datas1.size() != datas2.size()) {
            return false;
        }
        if (!datas1.containsAll(datas2)) {
            return false;
        }
        return true;
    }

    public static List getList(String[] str) {
        if(str == null){
            return null;
        }
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList(str));
        return list;
    }

    public static String[] concatString(String[]... data) {
        List<String> datas = new ArrayList<>();
        for (String[] result : data) {
            datas.addAll(Arrays.asList(result));
        }
        String[] array = datas.toArray(new String[datas.size()]);
        return array;
    }

    public static void showLog(String tag, String msg){
        int showCount = 500;
        if(msg.length() >showCount){
            String show = msg.substring(0, showCount);
            Log.i(tag, show + "");
            if((msg.length() - showCount) > showCount){//剩下的文本还是大于规定长度
                String partLog = msg.substring(showCount,msg.length());
                showLog(tag, partLog);
            }else{
                String surplusLog = msg.substring(showCount, msg.length());
                Log.i(tag, surplusLog+"");
            }
        }else{
            Log.i(tag, msg+ "");
        }
    }
}
